package com.cnblogs.hoojo.code.util;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * <b>function:</b>
 * 
 * @author hoojo
 * @createDate 2017-5-24 下午1:26:10
 * @file NamedUtils.java
 * @package com.cnblogs.hoojo.code.util
 * @project MyGenCode
 * @blog http://blog.csdn.net/IBM_hoojo
 * @email hoojo_@126.com
 * @version 1.0
 */
public class NamedUtils {

	public static String recamelName(String underline) {
		return NamingStrategy.capitalFirst(NamingStrategy.underlineToCamel(underline));
	}

	public static String camelName(String underline) {
		return NamingStrategy.underlineToCamel(underline);
	}

	public static String lowerName(String name) {
		if (StringUtils.isNotEmpty(name)) {
			return name.substring(0, 1).toLowerCase() + name.substring(1);
		}
		return "";
	}
	
	public static String abbreviation(String name) {
		if (StringUtils.isNotEmpty(name)) {
			StringBuffer sb = new StringBuffer();
			
			String[] names = StringUtils.split(name, "_");
			for (String item : names) {
				sb.append(StringUtils.substring(item, 0, 1));
			}
			return StringUtils.lowerCase(sb.toString());
		}
		return "";
	}
	
	/**
     * <b>function:</b> 转换为下划线命名方式
     * @author hoojo
     * @createDate 2017-1-20 下午5:24:50
     */
    public static String underscoreName(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && name.length() > 0) {
            // 将第一个字符处理成大写
            result.append(name.substring(0, 1).toUpperCase());
            // 循环处理其余字符
            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                // 在大写字母前添加下划线
                if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
                    result.append("_");
                }
                // 其他字符直接转成大写
                result.append(s.toUpperCase());
            }
        }
        return result.toString().toLowerCase();
    }
    
    public static String centerlineName(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && name.length() > 0) {
            // 将第一个字符处理成大写
            result.append(name.substring(0, 1).toUpperCase());
            // 循环处理其余字符
            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                // 在大写字母前添加下划线
                if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
                    result.append("-");
                }
                // 其他字符直接转成大写
                result.append(s.toUpperCase());
            }
        }
        return result.toString().toLowerCase();
    }
    
    /**
     * <b>function:</b> 驼峰式命名方式
     * @author hoojo
     * @createDate 2017-1-20 下午5:25:09
     */
    public static String tocamelName(String name) {
        StringBuilder sb = new StringBuilder();
        if (!name.contains("_")) {
            // 不含下划线，仅将首字母小写
            return name.substring(0, 1).toLowerCase() + name.substring(1);
        }
        // 用下划线将原始字符串分割
        String camels[] = name.split("_");
        for (String camel :  camels) {
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty()) {
                continue;
            }
            // 处理真正的驼峰片段
            if (sb.length() == 0) {
                // 第一个驼峰片段，全部字母都小写
                sb.append(camel.toLowerCase());
            } else {
                // 其他的驼峰片段，首字母大写
                sb.append(camel.substring(0, 1).toUpperCase());
                sb.append(camel.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }
    
    
	
	public static void main(String[] args) {
		System.out.println(abbreviation("school_class_grade"));
	    System.out.println(abbreviation("school"));
	    
	    System.out.println(centerlineName("SchoolClassTeacher"));
	    System.out.println(centerlineName("SchoolroomTeacher"));
	    System.out.println(centerlineName("school"));
	    System.out.println(centerlineName("School"));
	    System.out.println(tocamelName("school_class_grade"));
	    System.out.println(tocamelName("school"));

	    System.out.println(underscoreName("School"));
	    System.out.println(underscoreName("SchoolClassTeacher"));
	    System.out.println(underscoreName("SchoolroomTeacher"));
    }
}
